package com.cly.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cly.base.contants.CacheKeysConstants;
import com.cly.entity.Post;
import com.cly.entity.PostCategory;
import com.cly.mapper.PostCategoryMapper;
import com.cly.result.JsonRequest;
import com.cly.result.exenum.RequestException;
import com.cly.service.IPostCategoryService;
import com.cly.service.IPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 岗位分类表 服务实现类
 *
 * @author by 春风能解释
 * <p>
 * 2024/11/12
 */
@Service
@RequiredArgsConstructor
public class PostCategoryServiceImpl extends ServiceImpl<PostCategoryMapper, PostCategory> implements IPostCategoryService {

    /**
     * 岗位分类列表
     */
    private final IPostService postService;

    /**
     * 分页查询岗位分类
     *
     * @param current      当前页码
     * @param size         每页显示数量
     * @param postCategory 条件
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.RecruitKeys.POST_CATEGORY_LIST, key = "#root.args")
    public JsonRequest<List<PostCategory>> getPostCategoryPage(Integer current, Integer size, PostCategory postCategory) {
        IPage<PostCategory> postCategoryPage = baseMapper.selectPostCategoryPage(new Page<>(current, size), postCategory);
        // 传入分页
        return JsonRequest.success(postCategoryPage.getRecords(), postCategoryPage.getTotal());
    }

    /**
     * 获取岗位分类树形结构
     *
     * @param PostCategory 条件
     * @return 岗位分类树形结构
     */
    @Override
    @Cacheable(value = CacheKeysConstants.RecruitKeys.POST_CATEGORY_TREE)
    public JsonRequest<List<Tree<String>>> getPostCategoryTree(PostCategory PostCategory) {
        List<PostCategory> list = baseMapper.selectPostCategory(PostCategory);
        List<TreeNode<String>> treeNodes = list.stream()
                .map(postCategory -> {
                    TreeNode<String> node = new TreeNode<>();
                    node.setId(postCategory.getId().toString());
                    node.setParentId(postCategory.getParentId().toString());
                    node.setName(postCategory.getName());
                    return node;
                })
                .toList();
        return JsonRequest.success(TreeUtil.build(treeNodes, "-1"));
    }

    /**
     * 新增岗位分类
     *
     * @param postCategory 新增的岗位分类
     * @return JSON
     */
    @Override
    @CacheEvict(value = {CacheKeysConstants.RecruitKeys.POST_CATEGORY_TREE, CacheKeysConstants.RecruitKeys.POST_CATEGORY_LIST}, allEntries = true)
    public JsonRequest<Boolean> addPostCategory(PostCategory postCategory) {
        return JsonRequest.success(save(postCategory));
    }

    /**
     * 删除岗位分类
     *
     * @param ids 分类ID
     * @return JSON
     */
    @Override
    @CacheEvict(value = {CacheKeysConstants.RecruitKeys.POST_CATEGORY_TREE, CacheKeysConstants.RecruitKeys.POST_CATEGORY_LIST}, allEntries = true)
    public JsonRequest<Boolean> deletePostCategory(List<Long> ids) {
        List<Long> cid = list(Wrappers.lambdaQuery(PostCategory.class)
                .select(PostCategory::getId)
                .in(PostCategory::getId, ids)//本级节点
                .or()
                .in(PostCategory::getParentId, ids))//获取子节点
                .stream()
                .map(PostCategory::getId)
                .toList();
        long count = postService.count(Wrappers.lambdaQuery(Post.class)
                .in(Post::getPostCategoryId, cid));
        if (count > 0) {
            return JsonRequest.error(RequestException.DATA_EXISTS_BINDING);
        }
        return JsonRequest.success(removeByIds(cid));
    }

    /**
     * 修改岗位分类
     *
     * @param postCategory 修改的岗位分类
     * @return JSON
     */
    @Override
    @CacheEvict(value = {CacheKeysConstants.RecruitKeys.POST_CATEGORY_TREE, CacheKeysConstants.RecruitKeys.POST_CATEGORY_LIST}, allEntries = true)
    public JsonRequest<Boolean> updatePostCategory(PostCategory postCategory) {
        PostCategory postCategory1 = getById(postCategory.getId());
        if (Objects.isNull(postCategory1)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        postCategory.setCreateTime(null);
        postCategory.setCreateUser(null);
        return JsonRequest.success(updateById(postCategory));
    }
}
